using Microsoft.EntityFrameworkCore;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Infrastructure.Persistence;
using UwinEducation.Shared.Models;

namespace UwinEducation.Infrastructure.Repositories
{
    /// <summary>
    /// 教师仓储实现
    /// </summary>
    public class TeacherRepository : Repository<Teacher, Guid>, ITeacherRepository
    {
        private readonly ITeacherAvailabilityRepository _teacherAvailabilityRepository;

        public TeacherRepository(ApplicationDbContext context, ITeacherAvailabilityRepository teacherAvailabilityRepository) : base(context)
        {
            _teacherAvailabilityRepository = teacherAvailabilityRepository;
        }

        /// <summary>
        /// 获取教师分页列表
        /// </summary>
        public async Task<PagedResult<Teacher>> GetPagedListAsync(TeacherPageRequest pageRequest)
        {
            var query = _context.Teachers.AsNoTracking().AsQueryable();

            //条件查询过来 老师档期
            if (pageRequest.StartDate.HasValue && pageRequest.EndDate.HasValue)
            {
                var teacherIds = await _teacherAvailabilityRepository.GetByDateRangeAsync(pageRequest.StartDate.Value, pageRequest.EndDate.Value)
                    .ContinueWith(t => t.Result.Select(a => a.TeacherId).Distinct().ToList());
                
                query = query.Where(t => teacherIds.Contains(t.Id));
            }

            if (!string.IsNullOrEmpty(pageRequest.TeacherNo))
            {
                query = query.Where(t => t.TeacherNo.Contains(pageRequest.TeacherNo));
            }
            if (!string.IsNullOrEmpty(pageRequest.Name))
            {
                query = query.Where(t => t.Name.Contains(pageRequest.Name));
            }
            if (!string.IsNullOrEmpty(pageRequest.Phone))
            {
                query = query.Where(t => t.Phone.Contains(pageRequest.Phone));
            }
            if (!string.IsNullOrEmpty(pageRequest.Email))
            {
                query = query.Where(t => t.Email.Contains(pageRequest.Email));
            }
            if (!string.IsNullOrEmpty(pageRequest.Remarks))
            {
                query = query.Where(t => t.Remarks.Contains(pageRequest.Remarks));
            }

            var total = await query.CountAsync();
            //query = query.OrderBy(t => int.Parse(t.TeacherNo));
            query = query.OrderBy(s => s.TeacherNo.PadLeft(6, '0'));
            var items = await query
                .Skip((pageRequest.PageIndex - 1) * pageRequest.PageSize)
                .Take(pageRequest.PageSize)
                .ToListAsync();

            return new PagedResult<Teacher>
            {
                Total = total,
                Items = items
            };
        }


        /// <summary>
        /// 获取教师详情
        /// </summary>
        public async Task<Teacher> GetDetailsByIdAsync(Guid id)
        {
            return await _context.Teachers
                .AsNoTracking()
                .FirstOrDefaultAsync(t => t.Id == id);
        }

        /// <summary>
        /// 检查教师工号是否存在
        /// </summary>
        public async Task<bool> ExistsByTeacherNoAsync(string teacherNo, Guid? excludeId = null)
        {
            var query = _context.Teachers.AsNoTracking().AsQueryable();
            if (excludeId.HasValue)
            {
                query = query.Where(t => t.Id != excludeId.Value);
            }
            return await query.AnyAsync(t => t.TeacherNo == teacherNo);
        }
    }
}